/*
 * @(#)ToolbarResourceManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.resources;

import org.eoti.swing.DefaultTool;
import org.eoti.swing.DefaultAction;

import javax.swing.*;
import java.util.concurrent.*;
import java.awt.event.*;

public class ToolbarResourceManager
extends GUIResourceManager
{
	protected static final String RES_TOOL_FLOAT="float";
	protected static final String RES_TOOL_ROLL="rollover";
	protected static final String RES_TOOL_ITEMS="items";
	protected static final String RES_TOOL_STYLE="style";

	protected static ConcurrentHashMap<String, JToolBar> tools;
	protected GUIResourceManager parentManager;

	public ToolbarResourceManager(GUIResourceManager parentManager, String bundleName, String resourceKey, JToolBar toolBar, MouseListener mouseListener)
	{
		super(bundleName, parentManager.getLocale());
		if(tools == null)
			tools = new ConcurrentHashMap<String, JToolBar>();

		String[] keys = getStrings(resourceKey);
		for(String key : keys)
		{
			if( (key == null) || (key.length() == 0))
			{
				toolBar.addSeparator();
				continue;
			}
			toolBar.add(getTools(key, mouseListener));
		}
		configureToolbar(toolBar, resourceKey);
	}

	protected void configureToolbar(JToolBar toolBar, String key)
	{
		toolBar.setFloatable(getBoolean(RES_FMT, key, RES_TOOL_FLOAT));
		toolBar.setRollover(getBoolean(RES_FMT, key, RES_TOOL_ROLL));
	}

	public static JToolBar getLoadedTool(String prefix)
	{
		if(tools == null) return null;
		return tools.get(prefix);
	}

	/**
     * Constructs a new JToolBar from the following keys:
	 * [prefix].items=action.undo|action.redo||action.cut|action.copy|action.paste
	 * where an empty item specifies a separator/spacer
	 * [prefix].float=false
	 * [prefix].rollover=true
	 * [prefix].style=SMALL_AND_TEXT
	 * with style as defined in DefaultTool.Style
	 *
	 * @param prefix to the keys in the resource
	 * @return DefaultTool constructed from the resource
	 * @param mouseListener to listen to mouse events
	 */
	public JToolBar getTools(String prefix, MouseListener mouseListener)
	{
		JToolBar toolBar = getLoadedTool(prefix);
		if(toolBar != null) return toolBar;

		toolBar = new JToolBar();

		String styleName = getString(RES_FMT, prefix, RES_TOOL_STYLE);
		DefaultTool.Style style = null;
		if(isDefined(styleName))
			style = DefaultTool.Style.valueOf(styleName);

		String[] keys = getStrings(RES_FMT, prefix, RES_TOOL_ITEMS);
		for(String key : keys)
		{
			if(!isDefined(key))
			{
				toolBar.addSeparator();
				continue;
			}

			DefaultAction action = ActionResourceManager.getLoadedAction(key);
			if(action != null)
			{
				DefaultTool tool = new DefaultTool(action);
				if(style != null)
					tool.setStyle(style);

				if(mouseListener != null)
					tool.addMouseListener(mouseListener);

				toolBar.add(tool);
				continue;
			}

			String TBSTRING = getString(RES_FMT, key, RES_TOOL_ITEMS);
			if(isDefined(TBSTRING))
			{
				JToolBar tb = getTools(key, mouseListener);
				if(tb != null)
				{
					toolBar.add(tb);
					continue;
				}
			}
		}
		configureToolbar(toolBar, prefix);
		tools.put(prefix, toolBar);
		return toolBar;
	}
}
